import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Component;
import java.text.DecimalFormat;

public class GUI extends JFrame {

	int buttonCounter = 0;
	
	private GridBagLayout layout;
	private GridBagConstraints constraints;
	
	// buttons for basic screen
	private JLabel[] greenLabels = new JLabel[6];
	private JLabel[] redLabels = new JLabel[6];
	private JLabel[] yellowLabels = new JLabel[6];
	// buttons for advanced screen
	private JLabel[] greenLabels2 = new JLabel[14];
	private JLabel[] redLabels2 = new JLabel[14];
	private JLabel[] yellowLabels2 = new JLabel[14];
	
	private JTextArea latencyText;
	private JTextArea dlText;
	private JTextArea ulText;
	private JTextArea qosTextBox;
	
	private static final String VOIP_STR = "VoIP capabilities may still be blocked by your ISP.";
	private static final String EMAIL_SEND = "You may not be able to receive Email";
	private static final String EMAIL_RECV = "You may not be able to send Email";
	private static final String FIREWALL = "You may be having some firewall issues.";
	private static final String INET_CONN = "Very limited connection. Your ports may be blocked.";
	private static final String LOADING = "Running Tests, please wait...";
	
	
	

	
	public GUI() {
		super("MIFC");
		constraints = new GridBagConstraints();
		
		JPanel panelBasic;
		JPanel panelAdvanced;
		JTabbedPane tabbedPane;
		
		// create basic panel
		panelBasic = new JPanel();
		panelBasic.setLayout(new GridBagLayout());
		
		// create advanced panel
		panelAdvanced = new JPanel();
		panelAdvanced.setLayout(new GridBagLayout());
		
		// create tabbed panel (combined)
		tabbedPane = new JTabbedPane();
		
		// create some blank labels that will be used to exactly specify the layout
		int blankCounter = 1;
		JLabel[] blankLabels = new JLabel[200];
		for (int i = 0; i < 200; i++) {
			blankLabels[i] = new JLabel("");
		}
		
		// create the start button
		JButton startButton = new JButton("Start");
		
		// create the select server JComboBox
		
		String names[] = { "Server1", "Server2" };
		JComboBox servers = new JComboBox(names);
		
		// the icons (lights) that will be used
		Icon greenLight = new ImageIcon( getClass().getResource("ButtonGreenSmall.gif"));
		Icon redLight = new ImageIcon( getClass().getResource("ButtonRedSmall.gif"));
		Icon yellowLight = new ImageIcon(getClass().getResource("ButtonYellowSmall.gif"));
		
		// turn icons into labels. Initially only red is visible
//		JLabel[] greenLabels = new JLabel[6];
//		JLabel[] redLabels = new JLabel[6];
//		JLabel[] yellowLabels = new JLabel[6];
		for (int i = 0; i < 6; i++) {
			greenLabels[i] = new JLabel("", greenLight, SwingConstants.CENTER);
			redLabels[i] = new JLabel("", redLight, SwingConstants.CENTER);
			yellowLabels[i] = new JLabel("", yellowLight, SwingConstants.CENTER);
			greenLabels[i].setVisible(false);
			redLabels[i].setVisible(true);
			yellowLabels[i].setVisible(false);
		}
		
		// now create some more of those labels for the advanced panel 
		// (again initially only red is visible)
//		JLabel[] greenLabels2 = new JLabel[14];
//		JLabel[] redLabels2 = new JLabel[14];
//		JLabel[] yellowLabels2 = new JLabel[14];
		for (int i = 0; i < 14; i++) {
			greenLabels2[i] = new JLabel("", greenLight, SwingConstants.CENTER);
			redLabels2[i] = new JLabel("", redLight, SwingConstants.CENTER);
			yellowLabels2[i] = new JLabel("", yellowLight, SwingConstants.CENTER);
			greenLabels2[i].setVisible(false);
			redLabels2[i].setVisible(true);
			yellowLabels2[i].setVisible(false);
		}
		
		// create the rest of the JLabels for basic panel
		JLabel internetConnection = new JLabel("Internet Connection");
		internetConnection.setFont(new Font("Calibri",Font.BOLD,25));
		JLabel webBrowsing = new JLabel("Web Browsing");
		webBrowsing.setFont(new Font("Calibri",Font.BOLD,25));
		JLabel email = new JLabel("E-mail");
		email.setFont(new Font("Calibri",Font.BOLD,25));
		JLabel voip = new JLabel("VoIP");
		voip.setFont(new Font("Calibri",Font.BOLD,25));
		JLabel internetRadio = new JLabel("Internet Radio");
		internetRadio.setFont(new Font("Calibri",Font.BOLD,25));
		JLabel videoStreaming = new JLabel("Video Streaming");
		videoStreaming.setFont(new Font("Calibri",Font.BOLD,25));
		
		// Create the textarea that will explain the specifics to the user (basic panel)
		qosTextBox = new JTextArea();
		
		// create the JLabels for advanced panel		
		JLabel http = new JLabel("HTTP");
		http.setFont(new Font("Calibri",Font.BOLD,25));
		JLabel https = new JLabel("HTTPS");
		https.setFont(new Font("Calibri",Font.BOLD,25));
		JLabel imap = new JLabel("IMAP");
		imap.setFont(new Font("Calibri",Font.BOLD,25));		
		JLabel imaps = new JLabel("IMAPS");
		imaps.setFont(new Font("Calibri",Font.BOLD,25));
		JLabel smtp = new JLabel("SMTP");
		smtp.setFont(new Font("Calibri",Font.BOLD,25));		
		JLabel smtps = new JLabel("SMTPS");
		smtps.setFont(new Font("Calibri",Font.BOLD,25));
		JLabel pop3 = new JLabel("POP3");
		pop3.setFont(new Font("Calibri",Font.BOLD,25));
		JLabel pop3s = new JLabel("POP3S");
		pop3s.setFont(new Font("Calibri",Font.BOLD,25));
		JLabel telnet = new JLabel("TELNET");
		telnet.setFont(new Font("Calibri",Font.BOLD,25));
		JLabel ssh = new JLabel("SSH");
		ssh.setFont(new Font("Calibri",Font.BOLD,25));
		JLabel multicast = new JLabel("Multicast");
		multicast.setFont(new Font("Calibri",Font.BOLD,25));
		JLabel vpn = new JLabel("VPN");
		vpn.setFont(new Font("Calibri",Font.BOLD,25));
		JLabel ftp = new JLabel("FTP");
		ftp.setFont(new Font("Calibri",Font.BOLD,25));
		JLabel ipv6 = new JLabel("IPv6");
		ipv6.setFont(new Font("Calibri",Font.BOLD,25));
		JLabel latency = new JLabel("Latency");
		latency.setFont(new Font("Calibri",Font.BOLD,25));
		JLabel dl = new JLabel("Download Speed");
		dl.setFont(new Font("Calibri",Font.BOLD,25));
		JLabel ul = new JLabel("Upload Speed");
		ul.setFont(new Font("Calibri",Font.BOLD,25));
		
		// crate the textareas that contain the values for latency and ul/dl speeds
		latencyText = new JTextArea();
		dlText = new JTextArea();
		ulText = new JTextArea();
				
		constraints.fill = GridBagConstraints.BOTH;
		constraints.weightx = 1;
		constraints.weighty = 1;
		
		// add components to basic panel
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 0,0,11,1); // first row
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 1,0,1,1); // second row
		addComponent(panelBasic, constraints, startButton, 1,1,4,1);
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 1,5,1,1);
		addComponent(panelBasic, constraints, servers, 1,6,5,1);
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 1,11,1,1);
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 2,0,12,2); // third and fourth row
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 4,0,1,1); // fifth row
		addComponent(panelBasic, constraints, internetConnection, 4,1,6,1);
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 4,7,1,1);
		addComponent(panelBasic, constraints, greenLabels[0], 4,8,2,1); 
		addComponent(panelBasic, constraints, yellowLabels[0], 4,8,2,1);
		addComponent(panelBasic, constraints, redLabels[0], 4,8,2,1);
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 4,10,2,1);
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 5,0,12,1); // sixth row
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 6,0,1,1); // seventh row
		addComponent(panelBasic, constraints, webBrowsing, 6,1,6,1); 
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 6,7,1,1);
		addComponent(panelBasic, constraints, greenLabels[1], 6,8,2,1);
		addComponent(panelBasic, constraints, yellowLabels[1], 6,8,2,1);
		addComponent(panelBasic, constraints, redLabels[1], 6,8,2,1);
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 6,10,2,1);
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 7,0,12,1); // 8th row
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 8,0,1,1); // 9th row
		addComponent(panelBasic, constraints, email, 8,1,6,1);
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 8,7,1,1);
		addComponent(panelBasic, constraints, greenLabels[2], 8,8,2,1);
		addComponent(panelBasic, constraints, yellowLabels[2], 8,8,2,1);
		addComponent(panelBasic, constraints, redLabels[2], 8,8,2,1);
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 8,10,2,1);
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 9,0,12,1); // 10th row
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 10,0,1,1); // 11th row
		addComponent(panelBasic, constraints, voip, 10,1,6,1);
		addComponent(panelBasic, constraints, blankLabels[blankCounter++],10,7,1,1);
		addComponent(panelBasic, constraints, greenLabels[3], 10,8,2,1);
		addComponent(panelBasic, constraints, yellowLabels[3], 10,8,2,1);
		addComponent(panelBasic, constraints, redLabels[3], 10,8,2,1);
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 10,10,2,1);
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 11,0,12,1); // 12th row
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 12,0,1,1); // 13th row
		addComponent(panelBasic, constraints, internetRadio, 12,1,6,1);
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 12,7,1,1);
		addComponent(panelBasic, constraints, greenLabels[4], 12,8,2,1);
		addComponent(panelBasic, constraints, yellowLabels[4], 12,8,2,1);
		addComponent(panelBasic, constraints, redLabels[4], 12,8,2,1);
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 12,10,2,1);
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 13,0,12,1); // 14th row
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 14,0,1,1); // 15th row
		addComponent(panelBasic, constraints, videoStreaming, 14,1,6,1);
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 14,7,1,1);
		addComponent(panelBasic, constraints, greenLabels[5], 14,8,2,1);
		addComponent(panelBasic, constraints, yellowLabels[5], 14,8,2,1);
		addComponent(panelBasic, constraints, redLabels[5], 14,8,2,1);
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 14,10,2,1);
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 15,0,12,1); // 16th row
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 16,0,1,2); // 17th & 18th row
		addComponent(panelBasic, constraints, qosTextBox, 16,1,10,2);
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 16,11,1,2);
		addComponent(panelBasic, constraints, blankLabels[blankCounter++], 18,0,12,1); //19th (final) row
		
		// add components to advanced panel
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 0,0,20,1); // 1st row
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 1,0,1,1); // 2nd row
		addComponent(panelAdvanced, constraints, http, 1,1,5,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 1,6,1,1); 
		addComponent(panelAdvanced, constraints, greenLabels2[0], 1,7,2,1);
		addComponent(panelAdvanced, constraints, redLabels2[0], 1,7,2,1);
		addComponent(panelAdvanced, constraints, yellowLabels2[0], 1,7,2,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 1,9,2,1);
		addComponent(panelAdvanced, constraints, https, 1,11,5,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 1,16,1,1);
		addComponent(panelAdvanced, constraints, greenLabels2[1], 1,17,2,1);
		addComponent(panelAdvanced, constraints, yellowLabels2[1], 1,17,2,1);
		addComponent(panelAdvanced, constraints, redLabels2[1], 1,17,2,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 1,19,1,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 2,0,20,1); // 3rd row
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 3,0,1,1); // 4th row
		addComponent(panelAdvanced, constraints, pop3, 3,1,5,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 3,6,1,1);
		addComponent(panelAdvanced, constraints, greenLabels2[2], 3,7,2,1);
		addComponent(panelAdvanced, constraints, redLabels2[2], 3,7,2,1);
		addComponent(panelAdvanced, constraints, yellowLabels2[2], 3,7,2,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 3,9,2,1);
		addComponent(panelAdvanced, constraints, pop3s, 3,11,5,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 3,16,1,1);
		addComponent(panelAdvanced, constraints, greenLabels2[3], 3,17,2,1);
		addComponent(panelAdvanced, constraints, redLabels2[3], 3,17,2,1);
		addComponent(panelAdvanced, constraints, yellowLabels2[3], 3,17,2,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 3,19,1,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 4,0,20,1); // 5th row
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 5,0,1,1); // 6th row
		addComponent(panelAdvanced, constraints, imap, 5,1,5,1); 
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 5,6,1,1);
		addComponent(panelAdvanced, constraints, greenLabels2[4], 5,7,2,1);
		addComponent(panelAdvanced, constraints, redLabels2[4], 5,7,2,1);
		addComponent(panelAdvanced, constraints, yellowLabels2[4], 5,7,2,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 5,9,2,1);
		addComponent(panelAdvanced, constraints, imaps, 5,11,5,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 5,16,1,1);
		addComponent(panelAdvanced, constraints, greenLabels2[5], 5,17,2,1);
		addComponent(panelAdvanced, constraints, redLabels2[5], 5,17,2,1);
		addComponent(panelAdvanced, constraints, yellowLabels2[5], 5,17,2,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 5,19,1,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 6,0,20,1); // 7th row
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 7,0,1,1);
		addComponent(panelAdvanced, constraints, smtp, 7,1,5,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 7,6,1,1);
		addComponent(panelAdvanced, constraints, greenLabels2[6], 7,7,2,1);
		addComponent(panelAdvanced, constraints, redLabels2[6], 7,7,2,1);
		addComponent(panelAdvanced, constraints, yellowLabels2[6], 7,7,2,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 7,9,2,1);
		addComponent(panelAdvanced, constraints, smtps, 7,11,5,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 7,16,1,1); 
		addComponent(panelAdvanced, constraints, greenLabels2[7], 7,17,2,1);
		addComponent(panelAdvanced, constraints, redLabels2[7], 7,17,2,1);
		addComponent(panelAdvanced, constraints, yellowLabels2[7], 7,17,2,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 7,19,2,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 8,0,20,1); // 8th row
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 9,0,1,1); // 9th row
		addComponent(panelAdvanced, constraints, telnet, 9,1,5,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 9,6,1,1);
		addComponent(panelAdvanced, constraints, greenLabels2[8], 9,7,2,1);
		addComponent(panelAdvanced, constraints, redLabels2[8], 9,7,2,1);
		addComponent(panelAdvanced, constraints, yellowLabels2[8], 9,7,2,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 9,9,2,1);
		addComponent(panelAdvanced, constraints, ssh, 9,11,5,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 9,16,2,1);
		addComponent(panelAdvanced, constraints, greenLabels2[9], 9,17,2,1);
		addComponent(panelAdvanced, constraints, yellowLabels2[9], 9,17,2,1);
		addComponent(panelAdvanced, constraints, redLabels2[9], 9,17,2,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 9,19,1,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 10,0,20,1); // 11th row 
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 11,0,1,1); // 12th row
		addComponent(panelAdvanced, constraints, multicast, 11,1,5,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 11,6,1,1);
		addComponent(panelAdvanced, constraints, greenLabels2[10], 11,7,2,1);
		addComponent(panelAdvanced, constraints, redLabels2[10], 11,7,2,1);
		addComponent(panelAdvanced, constraints, yellowLabels2[10], 11,7,2,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 11,9,2,1); // 
		addComponent(panelAdvanced, constraints, ipv6, 11,11,5,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 11,16,1,1);
		addComponent(panelAdvanced, constraints, greenLabels2[11], 11,17,2,1);
		addComponent(panelAdvanced, constraints, redLabels2[11], 11,17,2,1);
		addComponent(panelAdvanced, constraints, yellowLabels2[11], 11,17,2,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 11,19,1,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 12,0,20,1); // 13th row
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 13,0,1,1); // 14th row
		addComponent(panelAdvanced, constraints, ftp, 13,1,5,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 13,6,1,1);
		addComponent(panelAdvanced, constraints, greenLabels2[12], 13,7,2,1);
		addComponent(panelAdvanced, constraints, redLabels2[12], 13,7,2,1);
		addComponent(panelAdvanced, constraints, yellowLabels2[12], 13,7,2,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 13,9,2,1);
		//addComponent(panelAdvanced, constraints, vpn, 13,11,5,1);
		//addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 13,16,1,1);
//		addComponent(panelAdvanced, constraints, greenLabels2[13], 13,17,2,1);
//		addComponent(panelAdvanced, constraints, yellowLabels2[13], 13,17,2,1);
//		addComponent(panelAdvanced, constraints, redLabels2[13], 13,17,2,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 13,19,1,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 14,0,20,2); // 15th row
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 16,0,1,1); // 16th row
		addComponent(panelAdvanced, constraints, latency, 16,1,5,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 16,6,1,1);
		addComponent(panelAdvanced, constraints, latencyText, 16,7,2,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 16,9,2,1);
		addComponent(panelAdvanced, constraints, dl, 16,11,2,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 16,17,1,1);
		addComponent(panelAdvanced, constraints, dlText, 16,17,2,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 16,19,1,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 17,0,11,1); // 17th row
		addComponent(panelAdvanced, constraints, ul, 17,11,5,1); 
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 17,16,1,1);
		addComponent(panelAdvanced, constraints, ulText, 17,17,2,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 17,19,1,1);
		addComponent(panelAdvanced, constraints, blankLabels[blankCounter++], 18,0,20,1); // 18th row 
				
		// add the panels to the tabbed panel
		tabbedPane.addTab("Basic Functionalities", null, panelBasic, "Basic Panel");
		tabbedPane.addTab("Advanced Functionalities", null, panelAdvanced, "Advanced Panel");
		
		// add tabbed panel itself (finally!)
		add(tabbedPane);
		
		ButtonHandler handler = new ButtonHandler();
		startButton.addActionListener(handler);
		
	}
		
	private void addComponent(JPanel panel, 
							  GridBagConstraints constraints,
							  Component component, 
							  int row, 
							  int column, 
							  int width, 
							  int height) {
		constraints.gridx = column;
		constraints.gridy = row;
		constraints.gridwidth = width;
		constraints.gridheight = height;
		panel.add(component, constraints);
	}
	
	private class ButtonHandler implements ActionListener {
				
		// helper class that sets the visibility of the buttons according to the results we got
		private void setButtons(ConnectionStatus status, JLabel red, JLabel yellow, JLabel green) {
			if (status.currentLightStatus == ConnectionStatus.lights.RED ) {
				red.setVisible(true);
				yellow.setVisible(false); 
				green.setVisible(false);
			} else if (status.currentLightStatus == ConnectionStatus.lights.YELLOW ) {
				red.setVisible(false);
				yellow.setVisible(true); 
				green.setVisible(false);
			} else if (status.currentLightStatus == ConnectionStatus.lights.GREEN ) {
				red.setVisible(false);
				yellow.setVisible(false); 
				green.setVisible(true);
			}			
		}
		
		public void actionPerformed(ActionEvent event) {
			
			qosTextBox.setFont(new Font("Calibri", Font.PLAIN, 15));
			qosTextBox.setText(LOADING);
			
			ResultsTable table = new ResultsTable();
			TestRunner testRunner = new TestRunner();
			table = testRunner.startTests();
			
			//qosTextBox.setText("");
								
			// HTTP ( xLabels2[0] )
			ConnectionStatus http_status = new ConnectionStatus();
			http_status = table.getConnectionStatus("http");
			setButtons(http_status, redLabels2[0], yellowLabels2[0], greenLabels2[0]);
			
			// HTTPS ( xLabels2[1] )
			ConnectionStatus https_status = new ConnectionStatus();
			https_status = table.getConnectionStatus("https");
			setButtons(https_status, redLabels2[1], yellowLabels2[1], greenLabels2[1]);			
			
			// POP3 ( xLabels2[2] )
			ConnectionStatus pop_status = new ConnectionStatus();
			pop_status = table.getConnectionStatus("pop");
			setButtons(pop_status, redLabels2[2], yellowLabels2[2], greenLabels2[2]);
			
			// POP3S ( xLabels2[3] )
			ConnectionStatus pops_status = new ConnectionStatus();
			pops_status = table.getConnectionStatus("pops");
			setButtons(pops_status, redLabels2[3], yellowLabels2[3], greenLabels2[3]);
			
			// IMAP ( xLabels2[4] )
			ConnectionStatus imap_status = new ConnectionStatus();
			imap_status = table.getConnectionStatus("imap");
			setButtons(imap_status, redLabels2[4], yellowLabels2[4], greenLabels2[4]);
			
			// IMAPS ( xLabels2[5] )
			ConnectionStatus imaps_status = new ConnectionStatus();
			imaps_status = table.getConnectionStatus("imaps");
			setButtons(imaps_status, redLabels2[5], yellowLabels2[5], greenLabels2[5]);
		
			// SMTP (xLabels2[6] )  
			// We need info from both smtp_25 and smtp_587
			ConnectionStatus smtp_25_status = new ConnectionStatus();
			smtp_25_status = table.getConnectionStatus("smtp_25");			
			ConnectionStatus smtp_587_status = new ConnectionStatus();
			smtp_587_status = table.getConnectionStatus("smtp_587");
			if ( smtp_25_status.currentLightStatus == ConnectionStatus.lights.RED &&
				 smtp_587_status.currentLightStatus	== ConnectionStatus.lights.RED ) {
				redLabels2[6].setVisible(true);
				yellowLabels2[6].setVisible(false);
				greenLabels2[6].setVisible(false);
			} else if (smtp_25_status.currentLightStatus == ConnectionStatus.lights.GREEN &&
				       smtp_587_status.currentLightStatus == ConnectionStatus.lights.GREEN ) {
				redLabels2[6].setVisible(false);
				yellowLabels2[6].setVisible(false);
				greenLabels2[6].setVisible(true);
			} else {
				redLabels2[6].setVisible(false);
				yellowLabels2[6].setVisible(true);
				greenLabels2[6].setVisible(false);	
			}

			// SMTPS ( xLabels2[7] )
			ConnectionStatus smtp_465_status = new ConnectionStatus();
			smtp_465_status = table.getConnectionStatus("smtp_465");
			setButtons(smtp_465_status, redLabels2[7], yellowLabels2[7], greenLabels2[7]);
			
			// Telnet ( xLabels2[8] )
			ConnectionStatus telnet_status = new ConnectionStatus();
			telnet_status = table.getConnectionStatus("telnet");
			setButtons(telnet_status, redLabels2[8], yellowLabels2[8], greenLabels2[8]);
			
			// SSH ( xLabels2[9] )
			ConnectionStatus ssh_status = new ConnectionStatus();
			ssh_status = table.getConnectionStatus("ssh");
			setButtons(ssh_status, redLabels2[9], yellowLabels2[9], greenLabels2[9]);

			// MultiCast ( xLabels2[10] )
			ConnectionStatus multicast_status = new ConnectionStatus();
			multicast_status = table.getConnectionStatus("multicast");
			setButtons(multicast_status, redLabels2[10], yellowLabels2[10], greenLabels2[10]);			
				
			// FTP ( xLabels2[12] )
			ConnectionStatus ftp_status = new ConnectionStatus();
			ftp_status = table.getConnectionStatus("ftp");
			
			double ul = 0.0;
			double dl = 0.0;
			int flag = 0;
			boolean uflag = false;
			boolean dflag = false;
			
			DecimalFormat df = new DecimalFormat("#.##");
			String ul_str = "NaN";
			String dl_str = "NaN";
			try {
				ul = Double.parseDouble(ftp_status.uploadSpeed);
				ul_str = df.format(ul).toString();
				flag = 1;
				uflag = true;
				if (Double.isNaN(ul)) {
					flag = 0;
					uflag = false;
				}
			} catch (Exception e) {
				;
			}
			try {
				dl = Double.parseDouble(ftp_status.downloadSpeed);
				dl_str = df.format(dl).toString();
				flag = 1;
				dflag = true;
				if (Double.isNaN(dl)) {
					flag = 0;
					dflag = false;
				}
			} catch (Exception e) {
				;
			}
			
			ulText.setFont(new Font("Calibri", Font.PLAIN, 15));
			ulText.setAlignmentX(CENTER_ALIGNMENT);
			ulText.setText(ul_str + " KB/s");
			dlText.setFont(new Font("Calibri", Font.PLAIN, 15));
			dlText.setAlignmentX(CENTER_ALIGNMENT);
			dlText.setText(dl_str + " KB/s");	
			
			if (flag == 1) {
				redLabels2[12].setVisible(false);
				yellowLabels2[12].setVisible(false);
				greenLabels2[12].setVisible(true);
			} else { 
				redLabels2[12].setVisible(true);
				yellowLabels2[12].setVisible(false);
				greenLabels2[12].setVisible(false);
			}
				
			// IPv6 ( xLabels2[11] )
			ConnectionStatus ipv6_status = new ConnectionStatus();
			ipv6_status = table.getConnectionStatus("ipv6");
			setButtons(ipv6_status, redLabels2[11], yellowLabels2[11], greenLabels2[11]);
			if (ipv6_status.ipv6_status) {
				redLabels2[11].setVisible(false);
				yellowLabels2[11].setVisible(false); 
				greenLabels2[11].setVisible(true);
			} else if (!ipv6_status.ipv6_status) {
				redLabels2[11].setVisible(true);
				yellowLabels2[11].setVisible(false); 
				greenLabels2[11].setVisible(false);
			}	
			
			ConnectionStatus ping_status = new ConnectionStatus();
			ping_status = table.getConnectionStatus("ping");
			System.out.println("Latency: " + ping_status.latency);
			latencyText.setFont(new Font("Calibri", Font.PLAIN, 15));
			latencyText.setAlignmentX(CENTER_ALIGNMENT);
			latencyText.setText(ping_status.latency + " ms");
			
			// Dealing with Basic panel
			
			String statusString = "";
			
			// Internet Connection
			
			if (!table.allRed()){
				// we have a valid connection
				greenLabels[0].setVisible(true);
				yellowLabels[0].setVisible(false);
				redLabels[0].setVisible(false);
			} else {
				try {
					Double latency = Double.parseDouble(latencyText.getText());
					greenLabels[0].setVisible(false);
					yellowLabels[0].setVisible(true);
					redLabels[0].setVisible(false);
					statusString += INET_CONN;
				} catch (Exception e) {
					greenLabels[0].setVisible(false);
					yellowLabels[0].setVisible(false);
					redLabels[0].setVisible(true);
				}
			}
			
			// web browsing
			
			if (redLabels2[0].isVisible() && redLabels2[1].isVisible()) {
				redLabels[1].setVisible(true);
				yellowLabels[1].setVisible(false);
				greenLabels[1].setVisible(false);
			} else if (greenLabels2[0].isVisible() && greenLabels2[1].isVisible()){
				redLabels[1].setVisible(false);
				yellowLabels[1].setVisible(false);
				greenLabels[1].setVisible(true);
			} else {
				redLabels[1].setVisible(false);
				yellowLabels[1].setVisible(true);
				greenLabels[1].setVisible(false);
			}
			
			// email received
			
			
			
			if (!(greenLabels2[2].isVisible() &&
				greenLabels2[3].isVisible() &&
				greenLabels2[4].isVisible() &&
				greenLabels2[5].isVisible()))
			{
				statusString += "\n" + EMAIL_RECV;
			}
			
			if (!(greenLabels2[6].isVisible() &&
				greenLabels2[7].isVisible()))
				{
					statusString += "\n" + EMAIL_SEND;
				}
			
			if (redLabels2[2].isVisible() &&
				redLabels2[3].isVisible() &&
				redLabels2[4].isVisible() &&
				redLabels2[5].isVisible() &&
				redLabels2[6].isVisible() &&
				redLabels2[7].isVisible()) {
				
				greenLabels[2].setVisible(false);
				yellowLabels[2].setVisible(false);
				redLabels[2].setVisible(true);
				
				
			} else if (	greenLabels2[2].isVisible() &&
						greenLabels2[3].isVisible() &&
						greenLabels2[4].isVisible() &&
						greenLabels2[5].isVisible() &&
						greenLabels2[6].isVisible() &&
						greenLabels2[7].isVisible()) {
				
				greenLabels[2].setVisible(true);
				yellowLabels[2].setVisible(false);
				redLabels[2].setVisible(false);
			} else {
				greenLabels[2].setVisible(false);
				yellowLabels[2].setVisible(true);
				redLabels[2].setVisible(false);
			}
			
			
			// parsing textfields
			Double latency = 5000.0;
			boolean lflag;
			
			try {
				latency = Double.parseDouble(ping_status.latency);
				lflag = true;
			} catch (Exception e){
				lflag = false;
			}
			
			// VoIP
			
			if (!lflag || !dflag || !uflag){
				greenLabels[3].setVisible(false);
				yellowLabels[3].setVisible(false);
				redLabels[3].setVisible(true);
				
			} else {
				if (ul > 100 && dl > 100 && latency < 500) {
					greenLabels[3].setVisible(true);
					yellowLabels[3].setVisible(false);
					redLabels[3].setVisible(false);
					statusString += ("\n" + VOIP_STR);
				} else if (ul < 30 || dl < 30 || latency > 1000){
					greenLabels[3].setVisible(false);
					yellowLabels[3].setVisible(false);
					redLabels[3].setVisible(true);	
				} else {
					greenLabels[3].setVisible(false);
					yellowLabels[3].setVisible(true);
					redLabels[3].setVisible(false);
				}
			}
			
			// Internet Radio
			
			if (!dflag) {
				greenLabels[4].setVisible(false);
				yellowLabels[4].setVisible(false);
				redLabels[4].setVisible(true);
			} else {
				if (dl > 20)
				{
					greenLabels[4].setVisible(true);
					yellowLabels[4].setVisible(false);
					redLabels[4].setVisible(false);
				} else if (dl < 10) {
					greenLabels[4].setVisible(false);
					yellowLabels[4].setVisible(false);
					redLabels[4].setVisible(true);
				} else {
					greenLabels[4].setVisible(true);
					yellowLabels[4].setVisible(false);
					redLabels[4].setVisible(false);
				}
			}
			
			// video streaming
			
			if (!lflag || !dflag || !uflag){
				greenLabels[5].setVisible(false);
				yellowLabels[5].setVisible(false);
				redLabels[5].setVisible(true);
				
			} else {
				if (ul > 300 && dl > 300 && latency < 500) {
					greenLabels[5].setVisible(true);
					yellowLabels[5].setVisible(false);
					redLabels[5].setVisible(false);
					statusString += ("\n" + VOIP_STR);
				} else if (ul < 128 || dl < 128 || latency > 800){
					greenLabels[5].setVisible(false);
					yellowLabels[5].setVisible(false);
					redLabels[5].setVisible(true);	
				} else {
					greenLabels[5].setVisible(false);
					yellowLabels[5].setVisible(true);
					redLabels[5].setVisible(false);
				}
			}
			
			// firewall issues
			boolean firewallflag = false;
			for (int x = 0; x < 10; x++){
				if(redLabels2[x].isVisible()){
					firewallflag = true;
					break;
				}
			}
			if (redLabels2[12].isVisible())
				firewallflag = true;
			
			if (firewallflag) {
				statusString += ("\n" + FIREWALL);
			}
			
			
			qosTextBox.setText(statusString);
			
			System.out.println("Button Pressed " + buttonCounter + " times");
			buttonCounter++;
		}
	}
}